/*
 * @Author: 贺楠 1071780426@qq.com
 * @Date: 2024-03-22 16:51:45
 * @LastEditors: 贺楠 1071780426@qq.com
 * @LastEditTime: 2024-03-22 17:29:27
 * @FilePath: \newMould_BHZ\src\components\PCManage\testEquipmentManage\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import {
    savePileFoundationTaskEquip, //保存
    getPileFoundationTaskEquipByFid, //详情
} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "fCompanyid",
        "showName"
    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            fCompanyids: null,
            fCompanynames: null,
            dialogHeight: null,
            formObj: formJs.formObj,
            formData: {
                userId: "", // 当前登陆用户id
                userName: "", // 当前登陆用户名称
                fId: "", // 数据fid  新增时不传或者传空 ,修改时传选中数据的fi
                fCompanyid: "",
                fCompanyname: "",
                fCheckcompanyname: "",
                fCheckequipname: ""
            },
            btnShow: true,
            currentDetail: null,
        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.fId
                        })
                    }
                }
            },
            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanyids = newVal;
                    this.formData.fCompanyid=newVal;
                }
            },
            immediate: true,
        },
        showName: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanynames = newVal;
                    this.formData.fCompanyname=newVal;
                    
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
        this.btnShow = true;
        if (this.statusNum == 1) { //新增
            this.formData.fCompanyid = this.userInfo.companyId; //当前登录人id
            this.formData.fCompanyname = this.userInfo.companyName; //当前登录人名称

            this.formData = this.$options.data.call(this).formData;
            this.btnShow = true
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;
            this.formData.fCompanyname=this.fCompanynames;
            this.formData.fCompanyid=this.fCompanyids;
            // this.formData.fProjectid = this.userInfo.projectId; //当前登录人
            // this.formData.fProjectname = this.userInfo.projectName; //当前登录人

        } else if (this.statusNum == 2) { //编辑
            this.formData.fCompanyname = this.currentDetail.fCompanyname; //当前登录人标段名称
            this.formData.fCompanyid = this.currentDetail.fCompanyid; //当前登录人标段ID
            this.formData.fId = this.currentDetail.fId //按钮 
            this.getDetail({
                fId: this.currentDetail.fId
            })
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;

        } else if (this.statusNum == 7) { //查看
            this.getDetail({
                fId: this.currentDetail.fId
            })
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.3 + "px";
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange() {},
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        // 详情
        getDetail(obj) {
            console.log(858,obj);
            this.loading = true;
            getPileFoundationTaskEquipByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data.equip
                    this.formData = data
                    if (this.statusNum == 7) {
                        this.btnShow = false
                        this.formObj.map((item) => {
                            item.disabled = true;
                        });
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },

        yj_saveForm() {

            // 0连续新增1新增
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(data) {
            this.loading = true;
            this.isLock = true
            this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
            // this.formData.fCompanyname = this.userInfo.companyName; //当前登录人标段名称
            // this.formData.fCompanyid = this.userInfo.companyId; //当前登录人标段ID


            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称


          
         

            savePileFoundationTaskEquip(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
        },
    },
};